<?php
	
	require_once(dirname(__FILE__) . "/../core/safeParam.php");
	
	if (isset($_GET['cid']) or isset($_GET['search'])) {
		$cid = $_GET['cid'];
		
		if (isset($_GET['search'])){
			$input = $_GET['search'];
  		}
  		
  		if (isset($input)) {
  			unset($results);
    		$search        = ' (name LIKE "%' . $input. '%" or type1 LIKE "%' . $input . '%" or code LIKE "%' . $input .'%" or description LIKE "%' . $input .'%") ';
    		$results_items = $database->select("SELECT * FROM menu_item WHERE  " . $search . " and rid = " . RID . " ");
    		
    		if ($results_items[0]['name'] != '') {
    			for ($h = 0; $h < count($results_items); $h++) {
        			$results_items[$h]['prices']    = unserialize($results_items[$h]['price']);
        			$results_items[$h]['optionsun'] = unserialize($results_items[$h]['options']);
        			$results_items[$h]['description'] = $results_items[$h]['description'];
      		} 
      		$results[0]['items'] = $results_items;
    		} else {
      		$noitems = 1;
    		}
    		
  		} else {
    		$results = $database->select("SELECT * FROM menu_cat WHERE id = $cid and rid = " . RID . " order by pos");
    		
    		for ($i = 0; $i < count($results); $i++) {
      		$price_types = unserialize($results[$i]['price']);      		
				$results[$i]['price_types'] = $price_types;
      		$results_items = $database->select("SELECT * FROM menu_item WHERE cat = '" . $results[$i]['id'] . "'");
      		
      		if ($results_items[0]['name'] != '') {
        			for ($h = 0; $h < count($results_items); $h++) {
        				$prices =  unserialize($results_items[$h]['price']);
          			// set up a hash with text key of price_type
          			$pricesHash = Array();
          			
          			for ($priceIndex = 0;$priceIndex < count($prices); $priceIndex++){
            			$priceName = $price_types[$priceIndex]; 
            			$priceValue = $prices[$priceIndex];
            			$pricesHash[$priceName] = $priceValue; 
          			}
          			
          			$sort = getOption("cb-category-list-price-sort-option", RID);

          			switch($sort) {
          				case "ascending by value":
              				asort($pricesHash);
              				break;
            			
            			case "descending by value":
              				arsort($pricesHash);
              				break;
            			
            			case "ascending by type":
              				ksort($pricesHash);
              				break;
            			
            			case "descending by type":
              				krsort($pricesHash);
              				break;
            			
            			default:
              				break;
          			}
						
						$displayPrices = getOption("cb-category-list-price-display-option", RID);
          			
          			if ($displayPrices == "first") {
              			$pricesHash = array_slice($pricesHash, 0, 1);
          			}
						
						$results_items[$h]['prices_hash'] = $pricesHash;
          			$results_items[$h]['optionsun'] = unserialize($results_items[$h]['options']);
          			$results_items[$h]['description'] = $results_items[$h]['description'];
        			} 
        			
        			$results[$i]['items'] = $results_items;
        			$noitems = 0;
      		} else {
        			$noitems = 1;
      		}
    		} 
  		}
  		
  		$smarty->assign('page_name', $results[0]['name']);
  		$smarty->assign('noitems', $noitems);
  		$smarty->assign('food_menu', $results);
	} 
?>
